/**
 * Created by ajhin on 7/11/2015.
 */
package com.societyGames2.convert
{
import org.as3commons.asblocks.parser.antlr.LinkedListTree;
import org.as3commons.asblocks.parser.antlr.as3.AS3Parser;

public class PackageTreeConverter implements ITreeConverter
{
  //ITreeToCSharp-->
  public function convert(tree:LinkedListTree):String
  {
    var typeTree:LinkedListTree;
    var blockTree:LinkedListTree;
    var childTree:LinkedListTree;
    for (var i:int = 0; tree.children != null && i < tree.children.length; i++)
    {
      childTree = LinkedListTree(tree.getChild(i));
      switch(childTree.type)
      {
        case AS3Parser.TYPE:
          typeTree = childTree;
          break;
        case AS3Parser.BLOCK:
          blockTree = childTree;
          break;
      }
    }
    var source:String = "package";
    if (typeTree != null)
    {
      source += " ";
      source += TreeConverters.convert(typeTree);
    }
    source += "\n" + TreeConverters.convert(blockTree);
    return source;
  }
  //<--ITreeToCSharp

  private function getChild(tree:LinkedListTree, type:int):LinkedListTree
  {
    var childTree:LinkedListTree;
    for (var i:int = 0; tree.children != null && i < tree.children.length; i++)
    {
      childTree = LinkedListTree(tree.getChild(i));
      if (childTree.type == type)
      {
        return childTree;
      }
    }
    return null;
  }
}
}
